<?php
/**
 * 展厅
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/28
 * Time: 22:50
 */
namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use Think\Db;

class Hall extends ApiBase
{
    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        $this->model = model('Hall');
    }

    /**
     * 展厅列表
     */
    public function getList(){
        $keyword = $this->request->param('keyword');
        $pavilion_id = $this->request->param('pavilion_id');
        $page = $this->request->param('page');

        $page = max(1,intval($page));
        $pagesize = 10;

        $where['is_delete'] = 0;
//        if(!empty($keyword)){
//            $where['title|number'] = array('like','%'.$keyword.'%');
//        }
//
//        if(!empty($pavilion_id)){
//            $where['pavilion_id'] = $pavilion_id;
//        }
//
//        $list = $this->model->listWherePage($where,'id desc',($page-1)*$pagesize,$pagesize);
//        $total = $this->model->countWhere($where);
        $whereOr = [];
        if(!empty($keyword)){
            $whereOr['title'] = array('like','%'.$keyword.'%');
//            $whereOr['title'] = array('like','%'.$keyword.'%','OR');
//            $whereOr['number'] = array('like','%'.$keyword.'%','OR');
        }

        if(!empty($pavilion_id)){
            $where['pavilion_id'] = $pavilion_id;
        }
        $list = $this->model->where($whereOr)->where($where)->order('id desc')->limit(($page-1)*$pagesize.','.$pagesize)->select();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['pavilion_title'] = db('pavilion')->where('id',$v['pavilion_id'])->value('title');
            }
            unset($v);
        }
        $total =  $this->model->where($whereOr)->where($where)->count();
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }

    /**
     * 展厅详情
     * @return \think\response\Json|void
     */
    public function detail(){
        $id = $this->request->param('id');
        $where['id'] = $id;
        $where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     *
     * @return mixed
     */
    public function _before_add(){
        $arr = ['title'=>'标题不能为空','pavilion_id'=>'请选择展馆','area'=>'请填写展厅面积','contact'=>'请填写联系人'];
        $data['title'] = $this->request->param('title');
        $data['pavilion_id'] = $this->request->param('pavilion_id');
        $data['area'] = $this->request->param('area');
        $data['status'] = $this->request->param('status');
        $data['contact'] = $this->request->param('contact');
        $data['mobile'] = input('mobile');
        $data['number']  = $this->request->param('number');
        $data['width']  = $this->request->param('width');
        $data['height']  = $this->request->param('height');
        $data['type']  = $this->request->param('type');
        $data['imgs']  = $this->request->param('imgs');
        $data['xs_area']  = $this->request->param('xs_area');

        $msg = '';
        foreach ($arr as $k=>$v){
            if(!isset($data[$k])||empty($data[$k])){
                $msg = $v;
                break;
            }
        }

        if(!empty($msg)){
            $this->returnJson([0,$msg]);
        }

        foreach ($data as $k=>$v){
            if(empty($v)){
                unset($data[$k]);
            }
        }

        return $data;
    }

    /**
     * 新增展厅
     * @return \think\response\Json|void
     */
    public function add(){
        $data = $this->_before_add();
        $data['create_time']  = time();
        $data['is_delete']  = 0;

        $r = $this->model->insertData($data);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }


    /**
     *
     * @return mixed
     */
    public function _before_edit(){
        $data['title'] = $this->request->param('title');
        $data['pavilion_id'] = $this->request->param('pavilion_id');
        $data['area'] = $this->request->param('area');
        $data['status'] = $this->request->param('status');
        $data['contact'] = $this->request->param('contact');
        $data['mobile'] = input('mobile');
        $data['number']  = $this->request->param('number');
        $data['imgs']  = $this->request->param('imgs');
        $data['width']  = $this->request->param('width');
        $data['height']  = $this->request->param('height');
        $data['type']  = $this->request->param('type');
        $data['xs_area']  = $this->request->param('xs_area');

        foreach ($data as $k=>$v){
            if(empty($v)){
                unset($data[$k]);
            }
        }

        return $data;
    }
    /**
     * 编辑展厅
     * @return \think\response\Json|void
     */
    public function edit(){
        $id = $this->request->param('id');
        $data = $this->_before_edit();

        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误，请核查']);
        }

        $where['id'] = $id;
        $r = $this->model->updateBywhere($data,$where);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    /**
     * 删除展厅
     * @return \think\response\Json|void
     */
    public function del(){
        $ids = $this->request->param('id');
        $id = explode(',',$ids);
        $is=true;$msg='';
        //首先进行数据验证
        foreach ($id as $val){
            $where = ['id'=>['in',$val],'is_delete'=>0];
            $r = $this->model->countWhere($where);
            if(empty($r)){
                $is=false;
                $msg="记录不存在，请核实";
                break;
            }
            $list=db('booth')->where(['hall_id'=>$val])->select();
            if(!empty($list)){
                $is=false;
                $msg="已绑定展位,不可删除";
                break;
            }
        }
        if(!$is){
            return json(['code'=>0,'msg'=>$msg]);
        }
        $msg='';
        foreach ($id as $v){
            $r = $this->model->deleteWhere(['id'=>$v]);
            if(!$r){
                $is=false;
                $msg='id:'.$v.'操作失败';
            }
        }

        if(!$is){
            Db::rollback();
            return json(['code'=>0,'msg'=>$msg]);
        }
        Db::commit();
        return json(['code'=>1,'msg'=>'操作成功']);
    }
}